package com.pet.pet03user.controller;

import com.pet.pojo.User;
import com.pet.service.UserService;
import com.pet.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login/{username}")
    public User getUserByName(@PathVariable("username") String username){
        return userService.getUserByUserName(username);
    }
    /*注册用户信息*/
    // @PreAuthorize("hasAuthority('sys:res:create')")
    @PostMapping("/register")
    public SysResult register(String username,String password,String repassword,String phone,String email){
        //判断两次密码输入是否一致
        if(!password.equals(repassword)) return SysResult.fail("两次输入密码不一致，请重新输入");
        User user = new User().setUsername(username).setEmail(email).setPassword(password).setStatus(0).setPhone(phone);
        //进入service层实现注册业务
        Boolean flag = userService.register(user);
        //如果返回false，说明用户名以被注册
        if(!flag) return SysResult.fail("用户名已注册，请重新登录");
        return SysResult.success();
    }

}
